<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #overlay {
      position: absolute;
      top: 0;
      left: 0;
      height: 400px;
      width: 800px;
      opacity: 0;
      z-index: 100;
      cursor: -webkit-grab;
      cursor: -moz-grab;
      cursor: grab;
      -moz-user-select: none;
      -ms-user-select: none;
      -webkit-user-select: none;
      user-select: none;
    }

    #container {
      height: 400px;
      width: 800px;
      background-color: #ccc;
      overflow: hidden;
    }

    #container:active {
      cursor: move;
      cursor: -webkit-grabbing;
      cursor: -moz-grabbing;
      cursor: grabbing;
    }

    .node {
      position: absolute;
      height: 20px;
      width: 20px;
      background-color: red;
      border-radius: 10px;
      pointer-events: none;
    }
  </style>
</head>

<body>
  <div id="overlay"></div>
  <div id="container">
    <div id="contents"></div>
  </div>

  <script>

    // ================================================
    // Increase or decrease this value for testing:
    var NUMBER_OF_OBJECTS = 40000;
    // Wether to use the overlay or the container directly
    var USE_OVERLAY = new URLSearchParams(location.search).get('use') === '1';
    // ================================================

    var overlay = document.getElementById("overlay");
    var container = document.getElementById("container");
    var contents = document.getElementById("contents");

    for (var i = 0; i < NUMBER_OF_OBJECTS; i++) {
      var node = document.createElement("div");
      node.innerHtml = i;
      node.className = "node";
      node.style.top = Math.abs(Math.random() * 2000) + "px";
      node.style.left = Math.abs(Math.random() * 2000) + "px";
      contents.appendChild(node);
    }

    var posX = 100;
    var posY = 100;
    var previousX = null;
    var previousY = null;

    var mousedownHandler = function (e) {
      window.onmousemove = globalMousemoveHandler;
      window.onmouseup = globalMouseupHandler;
      previousX = e.clientX;
      previousY = e.clientY;
    }

    var globalMousemoveHandler = function (e) {
      posX += e.clientX - previousX;
      posY += e.clientY - previousY;
      previousX = e.clientX;
      previousY = e.clientY;
      contents.style.transform = "translate3d(" + posX + "px, " + posY + "px, 0)";
    }

    var globalMouseupHandler = function (e) {
      window.onmousemove = null;
      window.onmouseup = null;
      previousX = null;
      previousY = null;
    }

    if (USE_OVERLAY) {
      overlay.onmousedown = mousedownHandler;
    } else {
      overlay.style.display = 'none';
      container.onmousedown = mousedownHandler;
    }


    contents.style.transform = "translate3d(" + posX + "px, " + posY + "px, 0)";
  </script>
</body>

</html>